package tz.preprocessing.lexical;

import java.io.File;
import java.io.FilenameFilter;
import java.util.LinkedList;
import java.util.List;

import tz.data.Article;
import tz.data.ArticleFilter;
import tz.data.ReutersArticle;

/**
 * 
 * @author dejvid
 * ZA routers
 */
public class WordExtract 
{

	public static List<Article> getAllArticles()
	{
		LinkedList<Article> lstArticles = new LinkedList<Article>();
		try
		{
			File articleDir = new File("data/reuters21578/");
			String[] fileList = articleDir.list(new FilenameFilter() {
				
				@Override
				public boolean accept(File file, String what)
				{
					if(what.endsWith(".sgm"))
						return true;
					else
						return false;
				}
			});
			for(String fileName : fileList)
			{
				lstArticles.addAll(ReutersXMLExtract.readAllArticles(new File(articleDir.getAbsolutePath()+File.separator+fileName)));
			}
			
		} catch (Exception ex)
		{
			ex.printStackTrace();
			System.err.println("**Napaka pri branju člankov");
		}
		return lstArticles;
	}
	
	public static List<String> getListOfWordsFromArticles(List<Article> lstArticles, ArticleFilter filter)
	{
		LinkedList<String> lstWords = new LinkedList<String>();
		for(Article article : lstArticles)
		{
			if(filter.useArticle(article))
			{
				article.getWordsFromArticle(lstWords);
			} //else skip article
		}
		return lstWords;
	}
	
	public static List<String> getListOfWordsFromArticle()
	{
		return null;
	}
	
	public static void main(String[] args) {
		List<Article> lst = getAllArticles();
		System.out.println(lst.size());
		for(Article a : lst)
		{
			System.out.print("Article id " + ((ReutersArticle)a).NEWID + " words:");
			List<String> lstWords = new LinkedList<String>();
			a.getWordsFromArticle(lstWords);
			for(String s : lstWords)
			{
				System.out.print(s + " ");
			}
			System.out.println();
		}
	}
}
